package com.jiuwan.gamepass.service.impl;

import com.jiuwan.gamepass.dao.AppDao;
import com.jiuwan.gamepass.dao.PicDao;
import com.jiuwan.gamepass.dto.appJson.ImageJson;
import com.jiuwan.gamepass.pojo.Pic;
import com.jiuwan.gamepass.service.PicService;
import com.jiuwan.gamepass.utils.MyCode;

import com.jiuwan.gamepass.utils.DeleteFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @version 1.0
 * 功能目的描述 ：用于         ========>>>>>>>>>>>
 * @author： pang-yun
 * @date： 2021-06-07 10:23
 */
@Slf4j
@Service
public class PicServiceImpl implements PicService {

    @Autowired
    private DeleteFileUtil deleteFileUtil;
    @Autowired
    private ImageJson imageJson;
    @Autowired
    private PicDao picDao;
    @Autowired
    private AppDao appDao;
    @Autowired
    private UploadServiceImpl uploadService;



    @Override
    public ImageJson selectPic(String appID) {
        if (appID==null){
            imageJson.setCode(MyCode.FAIL_CODE);
            imageJson.setMsg(MyCode.FAIL_MSG+"未输入参数");
            return imageJson;
        }
        List<Pic> pics = picDao.selectByPicturesid(appID);
        imageJson.setCode(MyCode.SUCCESS_CODE);
        imageJson.setMsg(MyCode.SUCCESS_MSG+"------------------");
        imageJson.setPicList(pics);
        return imageJson;
    }

    @Override
    public ImageJson updatePic(MultipartFile[] files, String appID) {
        ImageJson imageJson = uploadService.uploadImage(files, appID);
        return imageJson;
    }

    @Override
    public ImageJson deletePic(String appId) {

        List<Pic> pics = picDao.selectByPicturesid(appId);
        log.info("现将 {}储存的图片删除 ", pics);


        log.info("==============删除位置后期需要自己更改" );
        for (Pic p : pics) {
            String url = p.getUrl();

            String fileName = "G:\\MyProgran\\";
            String string = url.split("8080/")[1].toString();
            fileName=fileName+string;

            boolean delete = DeleteFileUtil.delete(fileName);

        }
        int i = picDao.deleteByPicturesid(appId);
        log.info("删除了{}条数据", i);
        imageJson.setCode(MyCode.SUCCESS_CODE);
        imageJson.setMsg(MyCode.SUCCESS_MSG+"list中为被删除的所有记录！");
        imageJson.setPicList(pics);
        return imageJson;

    }
}
